<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .w {
            width: 1200px;

            background-color: #fff;
            margin: 8px auto;


        }

        .header {
            height: 150px;
            background-color: purple;
        }

        .banner {
            height: 300px;
            background-color: skyblue;

        }

        .main {
            height: 1200px;
            background-color: seagreen;
        }

        .slider-bar {
            position: absolute;
            top: 300px;
            right: 34px;
            width: 40px;
            height: 140px;
            background-color: pink;
        }

        .goBack {
            display: none;

        }
    </style>
</head>

<body>
    <div class="slider-bar">
        <span class="goBack">返回顶部</span>
    </div>
    <div class="header w">头部区域</div>
    <div class="banner w">banner区域</div>
    <div class="main  w">主体部分</div>
    <script>
        var sliderBar = document.querySelector('.slider-bar')
        var banner = document.querySelector('.banner')
        var goBack = document.querySelector('.goBack')
        var main = document.querySelector('.main')

        var bannerTop = banner.offsetTop
        var sliderBarTop = sliderBar.offsetTop - bannerTop
        var mainTop = main.offsetTop

        document.addEventListener('scroll', function () {
            if (window.pageYOffset >= bannerTop) {
                sliderBar.style.position = 'fixed'
                sliderBar.style.top = sliderBarTop + 'px'

            } else if (window.pageYOffset <= bannerTop) {
                sliderBar.style.position = 'absolute'
                sliderBar.style.top = 300 + 'px'
            }
            if (window.pageYOffset >= mainTop) {
                goBack.style.display = 'block'
            } else if (window.pageYOffset <= mainTop) {
                goBack.style.display = 'none'

            }
        })
        goBack.addEventListener('click', function () {
            animate(window, 0)
        })

        function animate(obj, target, callback) {
            // 避免重复点击事件 导致定时器加速 每执行定时器时先清除上一个定时器
            clearInterval(obj.timer)
            obj.timer = setInterval(function () {
                // 步长值写在定时器里面
                var step = (target - window.pageYOffset) / 10;
                // 获得的步长值为正数则往上取整 负数往下取整
                step = step >= 0 ? Math.ceil(step) : Math.floor(step)
                if (window.pageYOffset == target) {
                    // 停止动画 本质是停止计时器
                    clearInterval(obj.timer)
                    // 定时器结束时如有回调函数就执行，没有就掠过不执行
                    // if (callback) {

                    //     callback()
                    // }
                    callback && callback()
                }
                // 步长公式step =（目标值 - 现在的位置）/10
                window.scroll(0, window.pageYOffset + step)
            }, 15)
        }


    </script>
</body>

</html>